package com.afkt.project.ui.adapter;

import com.afkt.project.model.bean.CommodityEvaluateBean;
import com.afkt.project.model.item.EvaluateItem;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.DevUtils;
import com.dev.assist.EditTextWatcherAssist;
import com.dev.base.widget.BaseEditText;
import com.dev.base.widget.BaseImageView;
import com.dev.base.widget.BaseTextView;
import com.dev.glide.utils.OhosGlide;
import com.dev.utils.common.BigDecimalUtils;
import com.dev.utils.common.StringUtils;
import ohos.agp.components.*;
import ohos.app.Context;


import java.io.IOException;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EditsAdapter extends BaseItemProvider {


    // EditText 输入监听辅助类
    private final EditTextWatcherAssist<EvaluateItem> editTextWatcherAssist = new EditTextWatcherAssist();

    private List<EvaluateItem> data = new ArrayList<>();
    private HashMap<Integer, String> map = new HashMap<>();
    private Context context;

    public EditsAdapter(List<EvaluateItem> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_adapter_item_edits, null, false);
        BaseTextView commodityName = (BaseTextView) component.findComponentById(ResourceTable.Id_vid_aie_name_tv);
        BaseTextView commodityPrice = (BaseTextView) component.findComponentById(ResourceTable.Id_vid_aie_price_tv);
        BaseImageView commodityImage = (BaseImageView) component.findComponentById(ResourceTable.Id_vid_aie_pic_igview);
        RatingBar vid_aie_ratingbar = new RatingBar((Image) component.findComponentById(ResourceTable.Id_starsOne)
                , (Image) component.findComponentById(ResourceTable.Id_starsTwo)
                , (Image) component.findComponentById(ResourceTable.Id_starsThree)
                , (Image) component.findComponentById(ResourceTable.Id_starsFour)
                , (Image) component.findComponentById(ResourceTable.Id_starsFive));
        BaseEditText vid_aie_content_edit = (BaseEditText) component.findComponentById(ResourceTable.Id_vid_aie_content_edit);
        BaseTextView vid_aie_number_tv = (BaseTextView) component.findComponentById(ResourceTable.Id_vid_aie_number_tv);
        CommodityEvaluateBean commodityEvaluateBean = data.get(i).commodityEvaluateBean;
        // ===========
        // = 商品信息 =
        // ===========
        commodityName.setText(commodityEvaluateBean.commodityName);
        // 跑马灯效果
        commodityName.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
// 始终处于自动滚动状态
        commodityName.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
// 启动跑马灯效果
        commodityName.startAutoScrolling();
        commodityPrice.setText("￥" + BigDecimalUtils.operation(commodityEvaluateBean.commodityPrice)
                .round(2, BigDecimal.ROUND_HALF_UP));
        commodityImage.setScaleMode(Image.ScaleMode.ZOOM_CENTER);

        try {
            OhosGlide.with(DevUtils.getAbilitySlice()).load(commodityEvaluateBean.commodityPicture).def(ResourceTable.Media_icon).into(commodityImage);
        } catch (IOException e) {
            e.printStackTrace();
        }

        vid_aie_ratingbar.setClick(i);
        vid_aie_ratingbar.setStar(data.get(i).evaluateLevel);

        // ===========
        // = 输入监听 =
        // ===========
        vid_aie_content_edit.setEditorActionListener(i1 -> {
            if(i1 ==InputAttribute.ENTER_KEY_TYPE_GO){
                vid_aie_content_edit.clearFocus();
            }
            return false;
        });
        vid_aie_number_tv.setText("" + (120 - StringUtils.length(data.get(i).evaluateContent)));
        map.put(i, data.get(i).evaluateContent);
        vid_aie_content_edit.setText(data.get(i).evaluateContent);
        editTextWatcherAssist.bindListener(data.get(i).evaluateContent, i, vid_aie_content_edit, (charSequence, editText, position, object) -> {
            try {
                // 保存评价内容
                //  getData().get(pos).evaluateContent = charSequence.toString();
                data.get(position).setEvaluateContent(charSequence.toString());
            } catch (Exception e) {
            }
            try {
                // 计算已经输入的内容长度
                vid_aie_number_tv.setText(String.valueOf(120 - editText.getText().length()));
            } catch (Exception e) {
                LogUtils.error("e=", e + "");
            }
        });

        return component;
    }

    public List<EvaluateItem> getData() {
        return data;
    }


    public class RatingBar implements Component.ClickedListener {
        List<Image> images = new ArrayList<>();
        int number=0;
        int position=0;

        public RatingBar(Image stars1, Image stars2, Image stars3, Image stars4, Image stars5) {
            images.add(null);
            images.add(stars1);
            images.add(stars2);
            images.add(stars3);
            images.add(stars4);
            images.add(stars5);
        }


        public void setClick(int position) {
            for (int i = 1; i <= 5; i++) {
                images.get(i).setClickedListener(this);
            }
            this.position=position;
        }

        public int setImages(int evaluateLevel) {
            if (evaluateLevel == 0) {
                for (int i = 1; i <= 5; i++) {
                    images.get(i).setPixelMap(ResourceTable.Media_icon_star_unselected);
                }
            } else {
                for (int i = 1; i <= 5; i++) {
                    if (i <= evaluateLevel) {
                        images.get(i).setPixelMap(ResourceTable.Media_icon_star_selected);
                    } else {
                        images.get(i).setPixelMap(ResourceTable.Media_icon_star_unselected);
                    }
                }
            }
            return evaluateLevel;
        }

        public void setStar(int evaluateLevel) {
            number= setImages(evaluateLevel);
        }

        @Override
        public void onClick(Component component) {
            switch (component.getId()) {
                case ResourceTable.Id_starsOne:
                    setStar(1);
                    break;
                case ResourceTable.Id_starsTwo:
                    setStar(2);
                    break;
                case ResourceTable.Id_starsThree:
                    setStar(3);
                    break;
                case ResourceTable.Id_starsFour:
                    setStar(4);
                    break;
                case ResourceTable.Id_starsFive:
                    setStar(5);
                    break;
            }
            data.get(position).evaluateLevel=number;
        }
    }

    /**
     * change start listener
     */
    public interface OnRatingChangeListener {

        void onRatingChange(int RatingCount);

    }


}
